package com.famdoc.Service.Impl;

import com.famdoc.Dao.SigningMapper;
import com.famdoc.Pojo.dto.PatientSigningForm;
import com.famdoc.Pojo.dto.PatientSigningHomeForm;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Service.SigningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SigningServiceImpl implements SigningService {

    @Autowired
    private SigningMapper signingMapper;

    /**
     * 个人签约
     * @param patientSigningForm
     * @return
     */
    @Override
    public Result signing(PatientSigningForm patientSigningForm) {
        //判断是否签约未过期，若未过期则不能签约
        Patient patient = signingMapper.queryPatientByIdNumber(patientSigningForm.getIdNumber());
        if (patient == null) {
            return Result.error(201,"该患者不存在");
        }
        Integer signYears = patient.getSignYears();
        LocalDateTime signTime = patient.getSignTime();
        if(signYears != null && signTime != null){
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 将签约年限添加到签约时间上
            LocalDateTime expirationTime = signTime.plusYears(signYears);

            // 比较当前时间是否在到期时间之前
            if (currentTime.isBefore(expirationTime)) {
                return Result.success("合同还未到期，到期时间是：" + expirationTime);
            }
        }

        //创建签约时间
        LocalDateTime localDateTime = LocalDateTime.now();
        patientSigningForm.setSignTime(localDateTime);
        //设置签约状态
        patientSigningForm.setSign(1);

        Integer integer=signingMapper.signing(patientSigningForm);
        if(integer==1) {
            return Result.success();
        }
        else {
            return Result.error(201, "签约失败，数据错误！");
        }
    }

    /**
     * 个人续约
     * @param patientSigningForm
     * @return
     */
    @Override
    public Result signed(PatientSigningForm patientSigningForm) {
        Patient patient = signingMapper.queryPatientByIdNumber(patientSigningForm.getIdNumber());
        if (patient == null) {
            return Result.error(201,"该患者不存在");
        }
        Integer signYears = patient.getSignYears();
        LocalDateTime signTime = patient.getSignTime();
        //判断患者是否已经签约
        if(signYears == null && signTime == null){
            return Result.success("该患者未签约！");
        }
        //判断是否签约过期，若过期则不能续约
        if(signYears != null && signTime != null){
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 将签约年限添加到签约时间上
            LocalDateTime expirationTime = signTime.plusYears(signYears);

            // 比较当前时间是否在到期时间之前
            if (currentTime.isAfter(expirationTime)) {
                return Result.success("合同已到期，到期时间是：" + expirationTime);
            }
        }
        //追加签约时间
        patientSigningForm.setSignYears(signYears+patientSigningForm.getSignYears());

        Integer integer=signingMapper.signed(patientSigningForm);
        if(integer==1) {
            return Result.success();
        }
        else {
            return Result.error(201, "续约失败，错误！");
        }
    }

    /**
     * 家庭签约
     * @param patientSigningHomeForm
     * @return
     */
    @Override
    public Result signingHome(PatientSigningHomeForm patientSigningHomeForm) {
        //判断是否签约未过期，若未过期则不能签约
        Patient patient = signingMapper.queryPatientByIdNumber(patientSigningHomeForm.getIdNumber());
        if (patient == null) {
            return Result.error(201,"该患者不存在");
        }
        Integer signHomeYears = patient.getSignHomeYears();
        LocalDateTime signHomeTime = patient.getSignHomeTime();
        if(signHomeYears != null && signHomeTime != null){
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 将签约年限添加到签约时间上
            LocalDateTime expirationTime = signHomeTime.plusYears(signHomeYears);

            // 比较当前时间是否在到期时间之前
            if (currentTime.isBefore(expirationTime)) {
                return Result.success("合同还未到期，到期时间是：" + expirationTime);
            }
        }

        //创建签约时间
        LocalDateTime localDateTime = LocalDateTime.now();
        patientSigningHomeForm.setSignHomeTime(localDateTime);
        //设置签约状态
        patientSigningHomeForm.setSignHome(1);

        Integer integer=signingMapper.signingHome(patientSigningHomeForm);
        if(integer==1) {
            return Result.success();
        }
        else {
            return Result.error(201, "签约失败，数据错误！");
        }
    }

    /**
     * 家庭续约
     * @param patientSigningHomeForm
     * @return
     */
    @Override
    public Result signedHome(PatientSigningHomeForm patientSigningHomeForm) {
        Patient patient = signingMapper.queryPatientByIdNumber(patientSigningHomeForm.getIdNumber());
        if (patient == null) {
            return Result.error(201,"该患者不存在");
        }
        Integer signHomeYears = patient.getSignHomeYears();
        LocalDateTime signHomeTime = patient.getSignHomeTime();
        //判断患者是否已经签约
        if(signHomeYears == null && signHomeTime == null){
            return Result.success("该家庭未签约！");
        }
        //判断是否签约过期，若过期则不能续约
        if(signHomeYears != null && signHomeTime != null){
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 将签约年限添加到签约时间上
            LocalDateTime expirationTime = signHomeTime.plusYears(signHomeYears);

            // 比较当前时间是否在到期时间之前
            if (currentTime.isAfter(expirationTime)) {
                return Result.success("合同已到期，到期时间是：" + expirationTime);
            }
        }
        //追加签约时间
        patientSigningHomeForm.setSignHomeYears(signHomeYears+patientSigningHomeForm.getSignHomeYears());

        Integer integer=signingMapper.signedHome(patientSigningHomeForm);
        if(integer==1) {
            return Result.success();
        } else {
            return Result.error(201, "续约失败，错误！");
        }
    }
}
